﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CRMTraceTool
{
    public partial class UserListForm : Form
    {
        public List<string> UserList { get; set; }
        string selectCheckBox = String.Empty;

        MainForm frm = null;

        public UserListForm(MainForm mainForm, List<string> userList, string selectedUser)
        {
            frm = mainForm;
            UserList = userList;
            selectCheckBox = selectedUser;
            
            InitializeComponent();
            addUserList();
        }

        private void addUserList()
        {
            int x = 13;
            int y = 13;
            int z = this.Width;

            System.Drawing.Point location = new Point(x,y);
            
            foreach (string u in UserList)
            {
                CheckBox cbUser = new CheckBox();
                cbUser.Name = u;
                cbUser.Text = u.ToUpper();
                cbUser.Width = 250;
                cbUser.Location = location;
                cbUser.Click += cbUser_Click;
                if (!String.IsNullOrEmpty(selectCheckBox) && u == selectCheckBox)
                {
                    cbUser.Checked = true;
                }
                y = y + 23;
                this.Controls.Add(cbUser);
                location.Y = y;
            }
        }

        void cbUser_Click(object sender, EventArgs e)
        {
            MainForm parent = (MainForm)this.Owner;
            CheckBox cb = (CheckBox)sender;
            if (String.IsNullOrEmpty(selectCheckBox) && cb.Checked)
            {
                selectCheckBox = cb.Name;
            }
            else if (selectCheckBox == cb.Name && !cb.Checked)
            {
                selectCheckBox = String.Empty;
            }
            else if (selectCheckBox != cb.Name)
            {
                Control[] cont = this.Controls.Find(selectCheckBox, false);
                CheckBox cb2 = (CheckBox)cont[0];
                cb2.Checked = false;
                selectCheckBox = cb.Name;
                parent.highlightUsers(selectCheckBox);
            }
            parent.highlightUsers(selectCheckBox);
        }
    }
}
